local skel = fk.CreateSkill {
  name = "rmt__daitian",
}

skel:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  prompt = "#rmt__daitian",
  target_tip = function (self, player, to, selected)
    if to:getMark("rmt__daitian_tar") ~= 0 then
      return "@@rmt__daitian_tar"
    elseif table.every(Fk:currentRoom().alive_players, function (p) return p == to or p.hp <= to.hp end) then
      return "@@rmt__daitian_max"
    end
  end,
  times = function (self, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to, selected)
    return #selected == 0 and (to:getMark("rmt__daitian_tar") ~= 0
    or table.every(Fk:currentRoom().alive_players, function (p)
      return p == to or p.hp <= to.hp
    end))
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    room:damage { from = player, to = to, damage = 1, skillName = skel.name }
  end,
})

-- 记录受伤角色
skel:addEffect(fk.Damaged, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.skillName == "rmt__daitian" and player:getMark("rmt__daitian_tar") == 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__daitian_tar", 1)
  end,
})

skel:addEffect(fk.Deathed, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.damage and data.damage.skillName == "rmt__daitian"
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#rmt__daitian-changeskill"})
  end,
  on_use = function (self, event, target, player, data)
    for _, scope in ipairs({Player.HistoryGame, Player.HistoryPhase, Player.HistoryRound, Player.HistoryTurn}) do
      player:setSkillUseHistory("rmt__shandian", player:usedSkillTimes("rmt__daitian", scope), scope)
    end
    player.room:handleAddLoseSkills(player, "-rmt__daitian|rmt__shandian", nil, false, true)
  end,
})

local RUtil = require ("packages.aaa_Romantic.utility.rmt_util")

skel:addEffect(fk.Deathed, {
  events = {fk.Deathed},
  can_trigger = function(self, event, target, player, data)
    if player.general == "rmt__lightningguy" or player.deputyGeneral == "rmt__lightningguy" then return false end
    if player:hasSkill(skel.name) or player:hasSkill("rmt__shandian") then
      if data.damage then
        return RUtil.isLightningDamage(data.damage)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#rmt__daitian-changehero"})
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerGeneral(player, "rmt__lightningguy")
    player:broadcastSkillInvoke("rmt__shandian")
    player.room:notifySkillInvoked(player, "rmt__shandian", "big")
  end,
})

Fk:loadTranslationTable{
  ["rmt__daitian"] = "代天",
  [":rmt__daitian"] = "出牌阶段限一次，你可以对体力值最高，或受到过“代天”伤害的角色造成1点伤害。因“代天”造成击杀后，你可以将此技能名改为“闪电”；因“闪电”造成击杀后，你可以将武将名改为“闪电”。",

  ["#rmt__daitian"] = "你可以对体力值最高，或受到过“代天”伤害的角色造成1点伤害",
  ["#rmt__daitian-changeskill"] = "你可以将技能“代天”改名为“闪电”",
  ["@@rmt__daitian_tar"] = "被代天过",
  ["@@rmt__daitian_max"] = "体力最高",
}

return skel
